﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Dictionary.Core;

namespace DictionaryConsole
{
    class Program
    {
        static bool saving = false;

        static void updateStatusDatabase()
        {
            while (saving)
            {
                Console.WriteLine("+ " + DateTime.Now.ToLongTimeString() + " : " + Category.count().ToString() + " Category(s) - " + Vocabulary.count().ToString() + " Vocab(s)");
                Thread.Sleep(5000);
            }
        }

        static void Main(string[] args)
        {
            String strSelect;
            bool exit = false;
            while (!exit)
            {
                Console.Clear();
                Console.WriteLine("MENU:");
                Console.WriteLine("-----");
                Console.WriteLine("1. Search dictionary.");
                Console.WriteLine("2. Auto update database.");
                Console.WriteLine("3. Exit");
                Console.Write("Select : ");
                strSelect = Console.ReadLine();

                switch (strSelect)
                {
                    case "1":
                        Console.Clear();
                        Console.Write("Enter keywork to search: ");
                        String strKeyword = Console.ReadLine();
                        if (strKeyword.Length > 0)
                        {
                            List<Vocabulary> listVocab = Vocabulary.search(0, strKeyword);
                            Console.WriteLine("Result '" + strKeyword + "' : " + listVocab.Count + " vocab(s). ");
                            foreach (Vocabulary itemVocab in listVocab)
                            {
                                Console.WriteLine("\t" + itemVocab.display());
                            }
                        }
                        Console.ReadLine();
                        break;
                    case "2":
                        Console.Clear();
                        Source source = new Source();
                        source.id = 1;
                        source.select();
                        Console.WriteLine("\n\nYou have : " + Category.count().ToString() + " Category(s) - " + Vocabulary.count().ToString() + " Vocab(s)");
                        Console.Write("URL: " + source.url + "\nDo you want to update database (this take a long time)? (y/n)");
                        String confirm = Console.ReadLine();
                        if (confirm.SequenceEqual("y"))
                        {
                            Console.WriteLine("\n\n\tBegin : " + DateTime.Now.ToLongTimeString());
                            // make thread to update status database
                            saving = true;
                            ThreadStart status = new ThreadStart(updateStatusDatabase);
                            Thread thread = new Thread(status);
                            thread.Start();
                            // To get and save to database
                            GetData getData = new GetData(source.url);
                            getData.Save();
                            // 
                            saving = false;
                            Console.WriteLine("\n\n\tEnd   : " + DateTime.Now.ToLongTimeString() + " Update database success.\nYou have : " + Category.count().ToString() + " Category(s) - " + Vocabulary.count().ToString() + " Vocab(s)");
                            Console.ReadLine();
                        }
                        break;
                    case "3":
                        exit = true;
                        break;
                }
                
            }
        }
    }
}
